package com.updesignsz.admin.mapper;

import com.updesignsz.commom.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * TODO
 * Created User: lx
 * Date: 2020/4/28 15:05
 */
@Repository
@Mapper
public interface RoleMapper {

    @Select("select r.*, r.id as roleId from t_role r where r.id IN (select role_id from t_user_role where user_id = #{userId}) order by created_date desc")
    @Results({
            @Result(property = "id", column = "roleId"),
            @Result(property = "functions", column = "id", many = @Many(select = "com.updesignsz.admin.mapper.FunctionMapper.findFunctionByRoleId"))
    })
    List<Role> findRoleByUserId(@Param("userId") Integer userId);


    @Select("select id, id as roleId, name, identifier, created_date, updated_date from t_role order by created_date desc")
    @Results({
            @Result(property = "id", column = "roleId"),
            @Result(property = "functions", column = "id", many = @Many(select = "com.updesignsz.admin.mapper.FunctionMapper.findFunctionByRoleId"))
    })
    List<Role> listAll();


    @Select("select id, id as roleId, name, identifier, created_date, updated_date from t_role where id = #{roleId}")
    @Results({
            @Result(property = "id", column = "roleId"),
            @Result(property = "functions", column = "id", many = @Many(select = "com.updesignsz.admin.mapper.FunctionMapper.findFunctionByRoleId"))
    })
    Role getByRoleId(@Param("roleId") Integer roleId);


    @Delete("delete from t_role where id = #{id}")
    void deleteRole(@Param("id") Integer id);

    @Insert({
            "insert into t_role(name, identifier, created_date, updated_date, is_removed) ",
            "values (#{name},#{identifier},NOW(),NOW(),0)"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveRole(Role role);
}
